<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.basic.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.insure.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.fee.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.live.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.school.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.service.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.notify.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.live.item.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/member/member.status.school.item.class.php');

class MemberStatusBase {
	var $basicObj = NULL;
	var $insureObj = NULL;
	var $feeObj = NULL;
	var $liveObj = NULL;
	var $schoolObj = NULL;
	var $serviceObj = NULL;
	
	var $notifyObj = NULL;
	var $notify = false;
	var $notify_types = array('basic', 'insure');
	var $version = 0;
	
	var $owner = 0;
	
	var $type = '';
	var $types = array('basic', 'insure', 'fee', 'live', 'school', 'service');
	var $item = array();
	var $items = array();
	
	//constructor
	function MemberStatusBase() {
		$this->notifyObj = new MemberStatusNotifyBase();
	}
	
	//setter/getter
	function setOwner($owner) {
		$this->owner = $owner;
	}
	
	function getOwner() {
		return $this->owner;
	}
	
	function setType($type) {
		if( in_array($type, $this->types) ) {
			$this->type = $type;	
		}
	}
	
	function getType() {
		return $this->type;
	}
	
	//
	function bindObj() {
		$item = array();
		if( in_array($this->type, $this->types) ) {
			$obj = $this->type . 'Obj';
			$class = 'MemberStatus' . ucfirst($this->type) . 'Base';
			$this->{$obj} = new $class();
			$this->{$obj}->bind($this->owner);
		}
		switch($this->type) {
			case 'basic':
				$this->basicObj = new MemberStatusBasicBase();
				$this->basicObj->bind($this->owner);
				break;
			case 'insure':
				$this->insureObj = new MemberStatusInsureBase();
				$this->insureObj->bind($this->owner);
				break;
			case 'fee':
				$this->feeObj = new MemberStatusFeeBase();
				$this->feeObj->bind($this->owner);
				break;
			case 'live':
				$this->liveObj = new MemberStatusLiveBase();
				$this->liveObj->bind($this->owner);
				break;
			case 'school':
				$this->schoolObj = new MemberStatusSchoolBase();
				$this->schoolObj->bind($this->owner);
				break;
			case 'service':
				$this->serviceObj = new MemberStatusServiceBase();
				$this->serviceObj->bind($this->owner);
				break;
		}
		
		return $item;
	}
	
	//methods
	function checkStatus() {
		return true;
	}
	
	function getItem() {
		$item = array();
		switch($this->type) {
			case 'basic':
				$obj = new MemberStatusBasicBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
			case 'insure':
				$obj = new MemberStatusInsureBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
			case 'fee':
				$obj = new MemberStatusFeeBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
			case 'live':
				$obj = new MemberStatusLiveBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
			case 'school':
				$obj = new MemberStatusSchoolBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
			case 'service':
				$obj = new MemberStatusServiceBase();
				$obj->bind($this->owner);
				$item = $obj->get_Item();
				break;
		}
		
		return $item;
	}
	
	function getItems() {
		$items = array();
		switch($this->type) {
			case 'live':
				$obj = new MemberStatusLiveItemBase();
				$obj->bind($this->owner);
				$items = $obj->getItems();
				break;
			case 'school':
				$obj = new MemberStatusSchoolItemBase();
				$obj->bind($this->owner);
				$items = $obj->getItems();
				break;
			case 'service':
				$obj = new MemberStatusServiceBase();
				$obj->bind($this->owner);
				$items = $obj->getItems();
				break;
		}
		
		return $items;
	}
	
	function updateItem($data_array) {
		$id = 0;
		$result = false;
		switch($this->type) {
			case 'basic':
				$obj = new MemberStatusBasicBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
			case 'insure':
				$obj = new MemberStatusInsureBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
			case 'fee':
				$obj = new MemberStatusFeeBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
			case 'live':
				$obj = new MemberStatusLiveBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
			case 'school':
				$obj = new MemberStatusSchoolBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
			case 'service':
				$obj = new MemberStatusServiceBase();
				$obj->bind($this->owner);
				$_id = $obj->get_ID();
				$_item = $obj->get_Item();
				if( $this->version == 0 && $_id > 0 ) {
					$result = $obj->updateItem($_id, $data_array);
				} else {
					$id = $obj->addItem($data_array);
				}
				break;
		}
		//
		if( $this->notify && in_array($this->type, $this->notify_types) ) {
			$d = array();
			$d['owner'] = $this->owner;
			$d['type'] = $this->type;
			$d['version'] = $this->version;
			$d['lastid'] = $_id;
			$d['lastitem'] = serialize($_item);
			if( $this->version == 0 && $_id > 0 && $result == true ) {
				$d['currentid'] = $_id;
			} else {
				$d['currentid'] = $id;
			}
			$item = $this->getItem();
			$d['currentitem'] = serialize($item);
			$diff = array();
			foreach($_item as $k => $v) {
				if( $v != $item[$k] && !in_array($k, array('date_added', 'added_by', 'last_modified', 'modified_by', 'status')) ) {
					$diff[$k] = array($v, $item[$k]);
				}
			}
			$d['difference'] = serialize($diff);
			$this->notifyObj->addNotify($d);
		}
		
		return $result;
	}
	
	//
	function getNotifyItems($filter = '', $check = 0, $limit = true) {
		return $this->notifyObj->getItems($filter, $check);
	}
	
	function getNotifyItemsTotal($filter = '', $check = 0) {
		return $this->notifyObj->getItemsTotal($filter, $check);
	}
}
//
?>